package com.gzsxy.esjy.service.statistical.controller;


import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.service.base.dto.response.StatisicalRsp;
import com.gzsxy.esjy.service.statistical.service.StatisicalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxl
 * @since 2022-03-16
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/admin/statisical")
@Api(description = "统计分析管理")
public class StatisicalController {

    @Autowired
    private StatisicalService statisicalService;

    private static final SimpleDateFormat DATE_FORMAT =  new SimpleDateFormat( "yyyy-MM-dd ");



    @ApiOperation("生成统计记录")
    @GetMapping("/getNum")
    public DefaultResponse getAllStatisicalByDay(@ApiParam("统计日期") @RequestParam(required = false) String day){
        StatisicalRsp statisicalRsp = statisicalService.getAllStatisicalByDay(day);
        return DefaultResponse.ok(statisicalRsp);
    }


    @ApiOperation("显示统计数据")
    @GetMapping("/show-chart")
    public DefaultResponse showChart(
            @ApiParam("开始时间") @RequestParam String begin,
            @ApiParam("结束时间") @RequestParam String end){
        Map<String, Map<String, Object>> map = statisicalService.getChartData(begin, end);
        return DefaultResponse.ok(map);
    }

    @ApiOperation("手动统计昨天整体的销量情况数据(仅当定时任务没有执行成功时调用)")
    @GetMapping("/ManualStatistics")
    public DefaultResponse LoginStatistics(){
        statisicalService.LoginStatistics();
        return DefaultResponse.ok();
    }


    @ApiOperation("初始化每日商家的订单销量数据(手动指定初始化指定天数，不对外仅测试)")
    @GetMapping("/initStatisicals/{date}")
    public DefaultResponse InitStatisicals(@PathVariable String date){
        statisicalService.InitStatisicals(date);
        return DefaultResponse.ok();
    }


}

